package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        roleMapper.deleteRelation(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Role role, Long[] ids) {

        roleMapper.insert(role);

        if(ids!=null && ids.length>0){
            //处理中间表
            for(Long permissionId:ids){
                roleMapper.insertRelation(role.getId(),permissionId);}
        }

    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> roles = roleMapper.selectAll();
        return roles;
    }

    @Override
    public void updateByPrimaryKey(Role role, Long[] ids) {

        roleMapper.deleteRelation(role.getId());
        if(ids!=null && ids.length>0){
            //处理中间表
            for(Long permissionId:ids){
                roleMapper.insertRelation(role.getId(),permissionId);}
        }
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Role> roles = roleMapper.selectForList(qo);
        //使用 分页插件,传入当前页,每页显示数量

        //调用startPage 方法后 ,会对下一句sql进行拦截,并进行改造成分页的 sql  千万注意哦
        return new PageInfo<Role>(roles);
    }

    @Override
    public List<String> selectSnByEmpId(Long id) {
        return null;
    }


}
